﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace PIM_ATE
{
    public partial class FrmUser : Form
    {
        public FrmUser(ClsDataUser userObj, int index = 0)
        {
            InitializeComponent();
            this.Text = "信息";
            m_userObj = userObj;
            cbTestItem.SelectedIndex = index;          

            Assembly asm = Assembly.GetExecutingAssembly();
            var asmName = asm.GetName();
            this.Text = asmName.Version.ToString();
        }

        ClsDataUser m_userObj;
        public  bool isNeedUser2 = false;

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            if (textBoxUser.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名ID！");
                return;
            }

            string userid2 = null, userpwd2 = null;
            if (isNeedUser2)
            {
                if (textBoxUser2.Text.Trim() == "")
                {
                    MessageBox.Show("请输入用户名ID！");
                    return;
                }
                userid2 = textBoxUser2.Text;
                userpwd2 = textPassword2.Text;
                if (userid2 == textBoxUser.Text)
                {
                    MessageBox.Show("账号重复！");
                    return;
                }
            }
                  

            if (cbTestItem.SelectedIndex == 0 && textBoxDarkroomNum.Text.Trim() == "")
            {
                MessageBox.Show("请输入暗室编号！");
                return;
            }


            if (!m_userObj.verifyUser(textBoxUser.Text, textPassword.Text, userid2, userpwd2))
            {
                MessageBox.Show(m_userObj.sError);
                return;
            }



            clsPowerManage.POWER_FEAFTURES Power;
            if (cbTestItem.SelectedIndex == 0)
                Power = clsPowerManage.POWER_FEAFTURES.PIM;
            else if (cbTestItem.SelectedIndex == 1)
                Power = clsPowerManage.POWER_FEAFTURES.ELEC;
            //else if (cbTestItem.SelectedIndex == 2)
            //    Power = clsPowerManage.POWER_FEAFTURES.PIM_CONFIG;
            //else if (cbTestItem.SelectedIndex == 3)
            //    Power = clsPowerManage.POWER_FEAFTURES.ELEC_CONFIG;
            else
                Power = clsPowerManage.POWER_FEAFTURES.PIM_CONFIG_SELECT;

            if (!m_userObj.HavePower(Power))
            {
                MessageBox.Show("权限不足！");
                return;
            }

            IniFile.SetFileName(System.Windows.Forms.Application.StartupPath + "\\JcDevices.ini");
            IniFile.IniWriteValue("Settings", "TestMode", cbTestItem.SelectedIndex.ToString());

            this.DialogResult = DialogResult.OK;
        }

        private void textBoxPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                BtnLogin_Click(sender, e);
            }
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            IniFile.SetFileName(System.Windows.Forms.Application.StartupPath + "\\JcDevices.ini");
            int index = 0;
            int.TryParse(IniFile.IniReadValue("Settings", "TestMode", "0"), out index);
            cbTestItem.SelectedIndex = index;        
            //textBoxDarkroomNum.Text = IniFile.IniReadValue("Settings", "DarkroomID", "0");
        }
        protected override void OnShown(EventArgs e)
        {
            //base.OnShown(e);
            textBoxUser2.Enabled = textPassword2.Enabled = isNeedUser2;
          //  textPassword.Enabled = textPassword2.Enabled = !GDefine.IS_USER_NOT_LOGIN;           
        }
        private void cbTestItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTestItem.SelectedIndex == 0)
            {
                label2.Visible = true;
                textBoxDarkroomNum.Visible = true;           
                isNeedUser2 = true;
            }
            else if (cbTestItem.SelectedIndex == 1)
            {
                label2.Visible = false;
                textBoxDarkroomNum.Visible = false;              
                isNeedUser2 = true;
            }
            else
            {
                label2.Visible = false;
                textBoxDarkroomNum.Visible = false;              
                isNeedUser2 = false;
            }
            textBoxUser2.Enabled = textPassword2.Enabled = isNeedUser2;
        }
    }
}
